package com.ys.controller.web.storage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.TccAssemble;
import com.ys.entity.storage.TccAssembleItem;
import com.ys.enume.storage.TccAssembleEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

/**
 * 组装报错单
 */
@Controller
@RequestMapping("/web/storage/tccAssemble")
public class WebTccAssembleController {

	@Autowired
	private CommonApiService<TccAssemble> commonApiService;

    /*
     * 异常组装
     */
    @GetMapping("/toError")
    public String toError(Model model, HttpServletRequest request, String bid) {
        try {
            Map<String, Object> extraParam = new TccAssemble()
            		.setBid(bid)
            		.addLeftJoin("TCG_PurchaseAppointOrderItem B ON B.Id = A.AppointOrderItemId")
            		.addField("B.ProductCode productCode")
            		.addField("B.ProductName productName")
            		.addField("B.CodeMerge codeMerge")
                    .addField("B.ReserveNumber parentPurchaseNumber")
            		
            		.queryFirstMap();
            
            TccAssemble entity = new TccAssemble().setBid(bid).queryFirst();
            if (entity.getSourceType() == TccAssembleEnum.SourceType.ERROR.value) {
				return W.error(model, "来源类型为异常组装，不能再次进行异常组装");
			}
			
			List<Map<String, Object>> sonlist = new TccAssembleItem()
					.setPbid(bid)
					.addLeftJoin("TCG_PurchaseInStorage B ON B.Id = A.PurchaseStorageId")
            		.addField("B.SubCode subCode")
            		.addField("B.SubName subName")
            		.addField("B.SubCodeMerge subCodeMerge")
            		.addField("B.SubBomNumber subBomNumber")
            		
					.queryMap();
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCC_Assemble, sonlist);
            commonApiService.toEdit(model, entity.getId(), entity, MenuId.TCC_Assemble, extraParam, subGridConfig);
            return "storage/tccAssemble/toError";
        }catch (Exception e) {
            return W.error(model, e.getMessage());
        }
    }


}






